package com.yami.shop.mystery.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.enums.StationEnum;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.mystery.common.model.MysteryBox;
import com.yami.shop.mystery.common.service.MysteryBoxService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 盲盒活动（平台端）
 *
 * @author wxf
 * @date 2023/4/11 18:28
 */
@RestController
@RequestMapping("/activity/mysteryBox")
@Tag(name="盲盒活动（平台端）")
public class MysteryBoxController {

    @Autowired
    private MysteryBoxService mysteryBoxService;

    @GetMapping("/page")
    @Operation(summary =  "分页查找盲盒盒子信息")
    public ServerResponseEntity<IPage<MysteryBox>> getMysteryBoxPage(PageParam<MysteryBox> page, @ParameterObject MysteryBox param) {
        IPage<MysteryBox> seckillPage = mysteryBoxService.getPlatformMysteryBoxPage(page, param);
        return ServerResponseEntity.success(seckillPage);
    }

    @GetMapping("/info/{id}")
    @Operation(summary =  "通过id查询盲盒盒子信息")
    @Parameter(name = "id", description = "盲盒盒子id", required = true)
    public ServerResponseEntity<MysteryBox> getById(@PathVariable("id") Long id) {
        MysteryBox coupon = mysteryBoxService.detail(id);
        return ServerResponseEntity.success(coupon);
    }

    @PutMapping("/invalid/{id}")
    @Operation(summary =  "通过id使盲盒盒子商品失效")
    @Parameter(name = "id", description = "盲盒盒子id", required = true)
    public ServerResponseEntity<Void> invalidById(@PathVariable Long id) {
        MysteryBox box = mysteryBoxService.getById(id);
        if (Objects.equals(box.getStatus(), StationEnum.OFFLINE.getValue())) {
            throw new YamiShopBindException("盲盒盒子已违规下架，无法关闭");
        }
        // 判断盲盒当前是否参加活动，如果是不能修改
        if (box.getProdType() != 0) {
            throw new YamiShopBindException("盲盒盒子正在参加活动，不能被下架");
        }
        mysteryBoxService.invalidById(box);
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary =  "通过id删除盲盒盒子信息")
    @Parameter(name = "id", description = "盲盒盒子id", required = true)
    public ServerResponseEntity<Void> removeById(@PathVariable Long id) {
        mysteryBoxService.deleteMysteryBox(id);
        return ServerResponseEntity.success();
    }

    // 获取盲盒列表
    @GetMapping("/getList")
    @Operation(summary =  "获取盲盒列表")
    public ServerResponseEntity<List<MysteryBox>> getList() {
        QueryWrapper<MysteryBox> mysteryBoxQueryWrapper = new QueryWrapper<>();
        mysteryBoxQueryWrapper.select("id","box_name");
        mysteryBoxQueryWrapper.eq("is_delete","0");
        List<MysteryBox> list = mysteryBoxService.list(mysteryBoxQueryWrapper);
        return ServerResponseEntity.success(list);
    }
}
